<?php
// 应用公共文件

use think\Response;
use think\exception\HttpResponseException;

// 应用公共文件
/**
 * 打印函数
 * @param  [type]  $str [description]
 * @param boolean $end [description]
 * @return void [type]       [description]
 */
function _p($str, $end = true)
{
    echo '<pre>';
    var_dump($str);
    if ($end) {
        die;
    }
}

/**
 * 生成默认返回数据
 * @param bool $return_data 返回数据是否生成data字段
 * @return mixed
 */
function returnData($return_data = true, $msg = 'request success', $code = 200, $success = true)
{
    $returnData[RESULT_CODE] = $code;
    $returnData[RESULT_SUCCESS] = $success;
    $returnData[RESULT_MESSAGE] = $msg;
    $return_data && $returnData[RESULT_DATA] = [];
    is_array($return_data) && $returnData[RESULT_DATA] = $return_data;
    return $returnData;
}

/**
 * 生成错误数据返回
 * @param bool $return_data
 * @param string $msg
 * @param int $code
 * @param bool $success
 * @return mixed
 */
function returnError($return_data = true, $msg = 'request error', $code = 500, $success = false)
{
    $returnData[RESULT_CODE] = $code;
    $returnData[RESULT_SUCCESS] = $success;
    $returnData[RESULT_MESSAGE] = $msg;
    $return_data && $returnData[RESULT_DATA] = [];
    is_array($return_data) && $returnData[RESULT_DATA] = $return_data;
    return $returnData;
}

/**
 * 返回实例名称
 */
function parseClass($logicName = '', $layerName = '', $moudel = 'common')
{
    $class = 'app\\' . $moudel . '\\' . $layerName . '\\' . $logicName;
    if (class_exists($class)) {
        $instance = $class;
    } else {
        $appName = App('http')->getName();
        $class = 'app\\' . $appName . '\\' . $layerName . '\\' . $logicName;
        if (class_exists($class)) {
            $instance = $class;
        } else {
            throw new \RuntimeException('class not exists:' . $class);
        }
    }
    return $instance;
}

/**
 * 抛出响应异常
 */
function throw_response_exception($data = [], $type = 'json')
{

    $response = Response::create($data, $type);

    throw new HttpResponseException($response);
}

/**
 * 生成毫秒级时间戳
 * @return integer
 */
function msectime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}

/**
 * 系统非常规MD5加密方法
 * @param string $str 要加密的字符串
 * @return string
 */
function data_md5($str, $key = 'ApiBase')
{

    return '' === $str ? '' : md5(sha1($str) . $key);
}

/**
 * 使用上面的函数与系统加密KEY完成字符串加密
 * @param string $str 要加密的字符串
 * @return string
 */
function data_md5_key($str, $key = '')
{
    if (is_array($str)) {
        ksort($str);
        $data = http_build_query($str);
    } else {
        $data = (string)$str;
    }
    return empty($key) ? data_md5($data, SYS_ENCRYPT_KEY) : data_md5($data, $key);
}

/**获取ip地址
 * @return array|false|mixed|string
 */
function getIPaddress()
{
    $IPaddress = '';
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $IPaddress = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $IPaddress = getenv("HTTP_CLIENT_IP");
        } else {
            $IPaddress = getenv("REMOTE_ADDR");
        }
    }
    return $IPaddress;
}
